<script setup lang="ts">
import { consultPayAPI } from '@/services/consult'
import { showLoadingToast } from 'vant'
import { ref } from 'vue'
interface Props {
  orderId: string
  payCallback: string
  show: boolean
}
const props = defineProps<Props>()

interface Emits {
  (name: 'update:show', value: boolean): void
}
const emit = defineEmits<Emits>()

const paymentMethod = ref<0 | 1>(1)
const onPay = async () => {
  showLoadingToast('跳转支付')
  const res = await consultPayAPI({
    orderId: props.orderId,
    payCallback: props.payCallback,
    paymentMethod: paymentMethod.value
  })
  window.location.href = res.data.payUrl
}
</script>

<template>
  <van-action-sheet
    :show="show"
    @update:show="emit('update:show', $event)"
    title="选择支付方式"
    :closeable="false"
    :close-on-popstate="false"
  >
    <div class="pay-type">
      <van-cell-group>
        <van-cell title="微信支付" @click="paymentMethod = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra><van-checkbox :checked="paymentMethod === 0" /></template>
        </van-cell>

        <van-cell title="支付宝支付" @click="paymentMethod = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra><van-checkbox :checked="paymentMethod === 1" /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="onPay">立即支付</van-button>
      </div>
    </div>
  </van-action-sheet>
</template>

<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
